/**
 * 时间 ： 2014-5-27 下午5:05:06
 * 作者 ： HaiTao
 * 描述 ： 当前类的描述信息
 **/
package com.laitaoo.service.home
{
	import com.laitaoo.event.HomeEvent;
	import com.laitaoo.service.BaseService;
	import com.maclema.mysql.MySqlResponse;
	import com.maclema.mysql.MySqlToken;
	import com.maclema.mysql.ResultSet;
	
	import mx.collections.ArrayCollection;
	import mx.graphics.Stroke;
	import mx.rpc.AsyncResponder;
	
	[RouteEvents(events="addProductResult,productListResult,getTotalInfoByDayIdResult")]
	[Component]
	public class AddProductService extends BaseService
	{
		public function AddProductService()
		{
			super();
		}
		/**
		 * 添加新的商品
		 * 
		 * @param product:String --- 商品名称
		 * @param kindId:int --- 种类ID
		 **/
		public function addProduct(product:String,kindId:int):void{
			
			var sql:String = "insert into product(productName,kindId) values ('"+product+"','"+kindId+"')";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(loginResultHandler,loginFaultHandler,token));
		}
		private function loginResultHandler(data:Object, token:Object):void{
			
			var rs:MySqlResponse = MySqlResponse(data);
			
			// 派发加载导航树的事件
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.ADDPRODUCTRESULT);
			dispatchEvent(homeEvent);
		}
		
		private function loginFaultHandler(info:Object, token:Object):void{
		}
		
		public function productList(dayId:int):void{
			
			var sql:String = "SELECT temp.productName,temp.cost,temp.dayId,temp.profit,temp.tunover,temp.remark,temp.pId,temp.kindId FROM (SELECT product.pId,product.productName,product.kindId,productValue.dayId,productValue.cost,productValue.productId,productValue.profit,productValue.tunover,productValue.remark FROM product LEFT JOIN productvalue ON product.pId=productvalue.productId and productValue.dayId='"+dayId+"') AS temp LEFT JOIN years ON temp.dayId=years.Id;";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(productListHandler,loginFaultHandler,token));
		}
		private function productListHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			var rows:ArrayCollection = rs.getRows() as ArrayCollection;
			
			// 派发加载导航树的事件
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.PRODUCTLISTRESULT);
			homeEvent.produtList = rows;
			dispatchEvent(homeEvent);
		}
		/**
		 * 保存编辑的数据
		 * 
		 * @param list:ArrayCollection --- 修改项目集合
		 * @param dayId:int --- 日期ID
		 * @param count:int --- list 索引
		 * */
		public function saveProduct(list:ArrayCollection,dayId:int,count:int = 0):void{

			if(count < list.length){
				
				var obj:Object = list.getItemAt(count);
				
				excute(obj,dayId,list,count);
			}
		}
		private function excute(obj:Object,dayId:int,list:ArrayCollection,count:int):void{
			
			var sql:String;
			
			var updateSql:String = "select * from productValue where productId='"+obj.Id+"' and dayId='"+dayId+"'";
			
			var token:MySqlToken = st.executeQuery(updateSql);
			
			token.addResponder(new AsyncResponder(function (data:Object, token:Object):void{
				
				var rs:ResultSet = ResultSet(data);
				
				if(rs.size() == 1){
					sql = "update productValue set cost='"+obj.cost+"',tunover='"+obj.tunover+"',profit='"+obj.profit+"',remark='"+obj.remark+"' where dayId='"+dayId+"' and productId='"+obj.Id+"'";
					updateProduct(sql,list,dayId,count);
				}else{
					sql = "insert into productValue(productId,cost,tunover,profit,remark,dayId) values('"+obj.Id+"','"+obj.cost+"','"+obj.tunover+"','"+obj.profit+"','"+obj.remark+"','"+dayId+"') ";
					insertProduct(sql,list,dayId,count);
				}
				
			},loginFaultHandler,token));
		}
		
		//更新
		private function updateProduct(sql:String,list:ArrayCollection,dayId:int,count:int):void{
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(function (data:Object,token:Object):void{
			
				var rs:MySqlResponse = MySqlResponse(data);
				
				count++;
				saveProduct(list,dayId,count);
				
			},loginFaultHandler,token));
		}
		
		// 添加
		private function insertProduct(sql:String,list:ArrayCollection,dayId:int,count:int):void{
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(function (data:Object,token:Object):void{
			
				var rs:MySqlResponse = MySqlResponse(data);
				
				count++;
				saveProduct(list,dayId,count);
				
			},loginFaultHandler,token));
		}
		/**
		 * 修改商品名称 
		 * 
		 * @param productName:String ---商品名称
		 * @param productId:int --- 商品ID
		 **/
		public function updateProductName(productName:String,productId:int):void{
			
			var sql:String = "update product set productName='"+productName+"' where Id='"+productId+"'";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(updateProductNameHandler,loginFaultHandler,token));
		}
		private function updateProductNameHandler(data:Object, token:Object):void{
			
			var rs:MySqlResponse = MySqlResponse(data);
		}
		/**
		 * 更新每天的汇总信息 
		 **/
		public function updateTotalInfoByDayId(totalCost:int,totalTunover:int,totalProfit:int,dayId:int):void{
			
			var sql:String = "select * from summary where dayId='"+dayId+"'";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(function (data:Object, token:Object):void{
			
				var rs:ResultSet = ResultSet(data);
				
				if(rs.size() == 0){
					sql = "insert into summary(totalCost,totalTunover,totalProfit,dayId) values ('"+totalCost+"','"+totalTunover+"','"+totalProfit+"','"+dayId+"')";
				}else{
					sql = "update summary set totalCost='"+totalCost+"',totalTunover='"+totalTunover+"',totalProfit='"+totalProfit+"' where dayId='"+dayId+"'";
				}
				excuteSummary(sql);
			},loginFaultHandler,token));
		}
		private function excuteSummary(sql:String):void{
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(updateSummaryByDayIdHandler,loginFaultHandler,token));
		}
		private function updateSummaryByDayIdHandler(data:Object, token:Object):void{
			
			var rs:MySqlResponse = MySqlResponse(data);
		}
		/**
		 * 获取每天的汇总信息 
		 **/
		public function getTotalInfoByDayId(dayId:int):void{
			
			var sql:String = "select * from summary where dayId='"+dayId+"'";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(getSummaryByDayIdHandler,loginFaultHandler,token));
		}
		private function getSummaryByDayIdHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			var rows:ArrayCollection = rs.getRows();
			
			var homeEvent:HomeEvent;
			
			if(rows.length > 0 ){
				
				var obj:Object = rows.getItemAt(0);
				
				homeEvent = new HomeEvent(HomeEvent.GETTOTALINFOBYDAYIDRESULT);
				homeEvent.totalCost = obj.totalCost;
				homeEvent.totalProfit = obj.totalProfit;
				homeEvent.totalTunover = obj.totalTunover;
				dispatchEvent(homeEvent);
			}else{
				
				homeEvent = new HomeEvent(HomeEvent.GETTOTALINFOBYDAYIDRESULT);
				homeEvent.totalCost = 0;
				homeEvent.totalProfit = 0;
				homeEvent.totalTunover = 0;
				dispatchEvent(homeEvent);
			}
		}
		
		public var tempList:ArrayCollection;
		/**
		 * 获取每月的统计数据
		 * */
		public function getMouthChart(mouthXml:XML,count:int = 0):void{

			var children:XMLList = mouthXml.children();
			
			if(count < children.length()){
				
				var xml:XML = children[count] as XML;
				getDayData(int(xml.@id),mouthXml,count);
				
			}else{
				
				// 派发加载导航树的事件
				var homeEvent:HomeEvent = new HomeEvent(HomeEvent.PRODUCTLISTRESULT);
				homeEvent.produtList = tempList;
				dispatchEvent(homeEvent);
				
				var obj:Object = new Object();
				
				for each (var tempObj:Object in tempList) 
				{
					obj.cost = int(tempObj.cost) + int(obj.cost);
					obj.profit = int(tempObj.profit) + int(obj.profit);
					obj.tunover = int(tempObj.tunover) + int(obj.tunover);
				}
				
				// 当月的汇总信息
				homeEvent = new HomeEvent(HomeEvent.GETTOTALINFOBYDAYIDRESULT);
				homeEvent.totalCost = obj.cost;
				homeEvent.totalProfit = obj.profit;
				homeEvent.totalTunover = obj.tunover;
				dispatchEvent(homeEvent);
			}
		}
		private function getDayData(dayId:int,mouthXml:XML,count:int = 0):void{
			
			var sql:String = "SELECT temp.productName,temp.cost,temp.dayId,temp.profit,temp.tunover,temp.remark,temp.pId,temp.kindId FROM (SELECT product.pId,product.productName,product.kindId,productValue.dayId,productValue.cost,productValue.productId,productValue.profit,productValue.tunover,productValue.remark FROM product LEFT JOIN productvalue ON product.pId=productvalue.productId and productValue.dayId='"+dayId+"') AS temp LEFT JOIN years ON temp.dayId=years.Id;";
		
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(function (data:Object, token:Object):void{
			
				var rs:ResultSet = ResultSet(data);
				
				var rows:ArrayCollection = rs.getRows() as ArrayCollection;
				
				getOneDayChartHandler(rows,mouthXml,count);
				
			},loginFaultHandler,token));
		}
		
		private function getOneDayChartHandler(list:ArrayCollection,mouthXml:XML,count:int):void{
			
			if(!tempList)
				tempList = list;
			
			for each (var tempObj:Object in tempList) 
			{
				for each (var obj:Object in list) 
				{
					if(tempObj.productName == obj.productName){
						tempObj.cost = int(tempObj.cost)+int(obj.cost);
						tempObj.tunover = int(tempObj.tunover)+int(obj.tunover);
						tempObj.profit = int(tempObj.profit)+int(obj.profit);
//						tempObj.remark = tempObj.remark+obj.remark;
						break;
					}
				}
			}
			count++;
			getMouthChart(mouthXml,count);
		}
	}
}

